﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SLua;
using UnityEngine.UI;

public class SpriteAssetManager : ModuleBase<SpriteAssetManager>
{
    private Dictionary<string, Vector4> m_spritePaddingMap = new Dictionary<string, Vector4>();
    private Dictionary<string, bool> m_spriteIsPoly = new Dictionary<string, bool>();
    private Dictionary<string, string> m_spriteAtlasMap = new Dictionary<string, string>();

    public SpriteAssetManager()
    {
    }

    public override void Init()
    {
        if (Config.Instance.UseTexturePacker)
        {
            LoadManager.Instance.LoadAsset(Config.UI_PATH, "atlas_padding", "asset", typeof(PaddingData), (data) =>
            {
                PaddingData pd = data as PaddingData;
                InitDataFromPaddingData(pd);
            }, false);
        }
    }

    void InitDataFromPaddingData(PaddingData pd)
    {
        if (pd == null)
        {
            return;
        }

        List<PaddingData.SpriteInfo> sprites = null;
        PaddingData.SpriteInfo info = null;
        for (int i = 0; i < pd.atlas.Count; ++i)
        {
            sprites = pd.atlas[i].sprites;
            bool isPoly = pd.atlas[i].isPoly;
            for (int j = 0; j < sprites.Count; ++j)
            {
                info = sprites[j];
                string name = info.name;
                AddSpriteAtlasInfo(name, pd.atlas[i].name);
                AddPaddingInfo(name, info.padding);
                AddIsPolyInfo(name, isPoly);
            }
        }
        //设置SDImage Padding
        SDImage.SetPaddingConfig(m_spritePaddingMap);
        SDImage.SetIsPolyConfig(m_spriteIsPoly);
    }

    public void AddSpriteAtlasInfo(string spriteName, string atlasName)
    {
        if (m_spriteAtlasMap.ContainsKey(spriteName))
        {
            m_spriteAtlasMap[spriteName] = atlasName;
        }
        else
        {
            m_spriteAtlasMap.Add(spriteName, atlasName);
        }
    }

    public void AddPaddingInfo(string k, Vector4 v)
    {
        if (m_spritePaddingMap.ContainsKey(k))
        {
            m_spritePaddingMap[k] = v;
        }
        else
        {
            m_spritePaddingMap.Add(k, v);
        }
    }
    public void AddIsPolyInfo(string k, bool v)
    {
        if (m_spriteIsPoly.ContainsKey(k))
        {
            m_spriteIsPoly[k] = v;
        }
        else
        {
            m_spriteIsPoly.Add(k, v);
        }
    }

    public string GetAtlasName(string spriteName)
    {
        if (!m_spriteAtlasMap.ContainsKey(spriteName))
        {
            Debuger.LogError(spriteName);
            return "";
        }
        return m_spriteAtlasMap[spriteName];
    }

    public bool HasSprite(string spriteName)
    {
        return m_spriteAtlasMap.ContainsKey(spriteName);
    }

    //    public Vector4 Get(string k)
    //    {
    //        if (m_spritePaddingMap.ContainsKey(k))
    //        {
    //            return m_spritePaddingMap[k];
    //        }
    //        return Vector4.zero;
    //    }
}

